const path = require('path');
const clean = require('clean-webpack-plugin');
const ExtractPlugin = require('extract-text-webpack-plugin');
const HTMLPlugin = require('html-webpack-plugin');
const webpack = require('webpack');
module.exports = {
    entry:{
        index:'./src/js/index.js'
    },
    output:{
        path:path.resolve(__dirname,'./dist'),
        filename:'./js/[name].[hash].js'
    },
    module:{
        rules:[
            {
                test:/\.js$/,
                use:{
                    loader:"babel-loader",
                    options:{
                        presets:["@babel/preset-env"]
                    }
                }
            },
            {
                test:/\.scss$/,
                use:ExtractPlugin.extract({
                    fallback:'style-loader',
                    use:['css-loader','sass-loader']
                })
                // use:['style-loader','css-loader','sass-loader']
            },
            {
                test:/\.(jpg|png|gif)/,
                use:{
                    loader:'url-loader',
                    options:{
                        limit:1024,
                        name:'./img/[name].[ext]',
                        publicPahtn:'/'
                    }
                }
            },
            {
                test:/\.html$/,
                use:{
                    loader:'html-loader',
                    options:{
                        attrs:[':src']
                    }
                }
            }
        ]
    },
    plugins:[
        new clean(),
        new ExtractPlugin({
            filename:'[name].css'
        }),
        new HTMLPlugin({
            template:'./src/index.html',
            inject:true,
            filename:'index.html',
            title:'首页'
        }),
        new webpack.HotModuleReplacementPlugin()
    ],
    devServer:{
        port:9090,
        host:"localhost",
        contentBase:'./dist',
        open:true,
        hot:true,
        before(app){
            app.get('api/list',(req,res,next) => {
                res.end('')
            })
        },
        proxy:{
            '/api':'http://localhost:8989'
        }
    },
    resolve:{
        alias:{
            'css':path.resolve(__dirname,'./src/scss')
        },
        extensions:['.js','.scss']
    },
    devtool:'cheap-module-eval-source-map'
}